package org.jeecg.modules.demo.crm.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.crm.dto.SelectDTO;
import org.jeecg.modules.demo.crm.entity.*;
import org.jeecg.modules.demo.crm.mapper.*;
import org.jeecg.modules.demo.crm.service.ICCommonService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.crm.service.ICInvoiceApplyService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 通用方法
 * @Author: jeecg-boot
 * @Date:   2025-06-24
 * @Version: V1.0
 */
@Api(tags="通用方法")
@RestController
@RequestMapping("/crm/cCommon")
@Slf4j
public class CCommonController extends JeecgController<CCommon, ICCommonService> {

	@Autowired
	private ICCommonService cCommonService;

	@Autowired
	private CProjectApplicationMapper cProjectApplicationMapper;

	@Autowired
	private CSubProjectMapper cSubProjectMapper;

	@Autowired
	private CContractorInfoMapper contractorInfoMapper;

	@Autowired
	private CCompanyInfoMapper companyInfoMapper;

	 @Autowired
	 private ICCommonService commonService;


	 /**
	  * 主项目 下拉选项
	  *
	  * @return
	  */
	 @ApiOperation(value="主项目下拉选项", notes="主项目下拉选项")
	 @GetMapping(value = "/projectList")
	 public Result<List<SelectDTO>> projectList() {
		 List<SelectDTO> list = new ArrayList<>();

		 QueryWrapper<CProjectApplication> queryWrapper = new QueryWrapper<>();
		 if(commonService.permissionCheck()){
			 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			 String projectId = cSubProjectMapper.projectPermissions(sysUser.getId());
			 if(projectId == null){
				 projectId = "-1";
			 }
			 List<Long> idList = Arrays.stream(projectId.split(","))
					 .map(String::trim)
					 .filter(StringUtils::isNotBlank)
					 .map(Long::valueOf)
					 .collect(Collectors.toList());
			 queryWrapper.in("id", idList);
		 }

		 List<CProjectApplication> cProjectApplications = cProjectApplicationMapper.selectList(queryWrapper);
		 cProjectApplications.stream().forEach(projectApplication -> {
			 SelectDTO selectDTO = SelectDTO.builder()
					 .value(projectApplication.getId())
					 .label(projectApplication.getProjectName())
					 .build();
			 list.add(selectDTO);
		 });
		 return Result.OK(list);
	 }

	 /**
	  * 子项目 下拉选择
	  * @return
	  */
	 @ApiOperation(value="子项目 下拉选择", notes="子项目 下拉选择")
	 @GetMapping(value = "/subProjectList")
	 public Result<List<SelectDTO>> subProjectList(@RequestParam(name="projectId",required=false) String projectId) {
		 List<SelectDTO> list = new ArrayList<>();
		 QueryWrapper<CSubProject> queryWrapper  = new QueryWrapper<>();
		 if(projectId != null){
			 queryWrapper.eq("project_id",projectId);
		 }

		 if(commonService.permissionCheck()){
			 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			 queryWrapper.eq("user_id",sysUser.getId());
		 }

		 List<CSubProject> subProjectList = cSubProjectMapper.selectList(queryWrapper);
		 subProjectList.stream().forEach(subProject -> {
			 SelectDTO selectDTO = SelectDTO.builder()
					 .value(subProject.getId())
					 .label(subProject.getSubProjectName())
					 .build();
			 list.add(selectDTO);
		 });
		 return Result.OK(list);
	 }


	 /**
	  * 分包商 下拉选择
	  * @return
	  */
	 @ApiOperation(value="分包商 下拉选择", notes="分包商 下拉选择")
	 @GetMapping(value = "/contractorList")
	 public Result<List<SelectDTO>> contractorList(@RequestParam(name="subProjectId",required=true) String subProjectId) {
		 List<SelectDTO> list = new ArrayList<>();
		 CSubProject subProjectList = cSubProjectMapper.selectById(subProjectId);
		 if(subProjectList != null && StringUtils.isNotEmpty(subProjectList.getContactId())){
			 CContractorInfo cContractorInfo = contractorInfoMapper.selectById(subProjectList.getContactId());
			 SelectDTO selectDTO = SelectDTO.builder()
					 .value(cContractorInfo.getId())
					 .label(cContractorInfo.getContactName())
					 .build();
			 list.add(selectDTO);
		 }

		 return Result.OK(list);
	 }


	 /**
	  * 总包 下拉选择
	  * @return
	  */
	 @ApiOperation(value="总包 下拉选择", notes="总包 下拉选择")
	 @GetMapping(value = "/companyInfoList")
	 public Result<List<SelectDTO>> companyInfoList(@RequestParam(name="projectId",required=true) String projectId) {
		 List<SelectDTO> list = new ArrayList<>();
		 CProjectApplication cProjectApplication = cProjectApplicationMapper.selectById(projectId);
		 if(cProjectApplication != null
				 && StringUtils.isNotEmpty(cProjectApplication.getCompanyInfoId())){
			 CCompanyInfo companyInfo = companyInfoMapper.selectById(cProjectApplication.getCompanyInfoId());
			 SelectDTO selectDTO = SelectDTO.builder()
					 .value(companyInfo.getId())
					 .label(companyInfo.getCompanyName())
					 .build();
			 list.add(selectDTO);
		 }
		 return Result.OK(list);
	 }

}
